// pages/searchEquip/searchEquip.js  
const app = getApp()
const util = require('../../../utils/util.js')
const lbbService = require('../../service/service.js')
var Base64 = require('../../../utils/base64.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    changeCodeShow: false,
    largeCode: '',
    smallCode: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
    let that = this;
    that.toast = that.selectComponent("#toast");
    that.personalModal = that.selectComponent("#personalModal");
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this;
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  hideChangeCodeModal: function () {
    this.setData({
      changeCodeShow: false,
      smallCode:'',
      largeCode:'',
      qc:''
    })
  },
  /**
   * 扫码获取控制盒编号
   */
  scanBoxCode: function () {
    let that = this;
    that.setData({
      changeCodeShow: true
    })
    wx.scanCode({
      success(res) {
        console.log('scanCode-->', res);
        if (!that.data.changeCodeShow) {
          that.setData({
            changeCodeShow: true
          })
        }
        let qrc = '';
        let qrCode = res.result;
        if (!qrCode) {
          that.toast.showToast('二维码为空');
          return;
        }
        let reg = /^([A-Za-z0-9]{2}:){5}([A-Za-z0-9]{2}){1}$/;
        if (qrCode.indexOf(util.CONST_DATA.DEVICE_QRCODE_URL_PREFIX) == 0 || qrCode.indexOf(util.CONST_DATA.DEVICE_QRCODE_OLDER_URL_PREFIX) == 0) {
          //控制盒大二维码                  
          lbbService.getQC({qc:qrCode},res=>{
            console.log('getQC-->',res);
            that.setData({
              largeCode:res.data.oldHpId,
              qc: qrCode
            })
          });      
        } else if (reg.test(qrCode)) {
          //控制盒小二维码
          that.data.deviceId = qrCode;
          that.data.mac = qrCode.replace(/:/g, '');
          lbbService.getHpIdByMacId({
            mac: that.data.mac
          }, res => {
            if (res.data&&res.data.pileId) {
              that.setData({
                smallCode: res.data.pileId,
                hospitalId: res.data.lbmId,
              })
            } else {
              that.toast.showToast('获取编号失败');
            }
          })
          return;
        } else {
          that.toast.showToast('二维码错误');
          return;
        }
      },
    })
  },
  /**
   * 绑定二维码
   */
  changeBrCode: function (e) {
    let that = this;
    console.log('largeCode>>', e);
    let qc = that.data.qc;
    let smallCode = e.detail.value.smallCode;
    that.setData({
      smallCode:smallCode
    })
    if (!smallCode||!qc) {
      that.toast.showToast('请扫码获取或输入编号','warning');
      return;
    }
    that.replaceBox(false);
  },
  replaceBox: function(isAllow){
    let that = this;
    lbbService.replaceBox({
      isAllow: isAllow,
      lbmId: 0,
      originHid: that.data.smallCode,
      qc: Base64.encode(that.data.qc),
      rType: 'qrcode',
    }, function (res) {
      that.toast.showToast('已绑定成功','success');
      setTimeout(()=>{
        that.hideChangeCodeModal();
      },2000)
    }, that.replaceBox)
  },

})